package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Doctorconsultations;
import com.qf.entity.Medication;

import com.qf.entity.Restoratives;

import com.qf.service.restoratives.MedicationService;
import com.qf.service.restoratives.RestorativesService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.qf.util.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * (Restoratives)表控制层
 *
 * @author makejava
 * @since 2024-12-04 17:10:28
 */
@RestController
@RequestMapping("/restoratives")
public class RestorativesController {
    /**
     * 服务对象
     */
    @Autowired
    private RestorativesService restorativesService;

    @Autowired
    private MedicationService medicationService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/list")
    public R queryByPage(Restoratives restoratives) {

        PageHelper.startPage(restoratives.getPageNum(), restoratives.getPageSize());

        List<Restoratives> list = restorativesService.list(restoratives);

        PageInfo<Restoratives> pageInfo = new PageInfo<>(list);

        return R.ok(pageInfo);
    }
    /**
     * 订单状态查询
     */
    @GetMapping("/orderStatus")
    public R orderStatus(Doctorconsultations doctorconsultations){

        List<Doctorconsultations> list = restorativesService.orderStatus(doctorconsultations);

        return R.ok(list);
    }

    /**
     * 查询药品订单详情
     */
    @GetMapping("/orderList")
    public R orderList(Medication medication){

        PageHelper.startPage(medication.getPageNum(), medication.getPageSize());

        List<Medication> list = medicationService.list(medication);

        PageInfo<Medication> pageInfo = new PageInfo<>(list);

        return R.ok(pageInfo);
    }
    /**
     * 导出药品订单详情excel表格
     */
    @GetMapping("/export/excel")
    public void export(HttpServletResponse response) throws IOException {
        Restoratives restoratives = new Restoratives();
        List<Restoratives> list = restorativesService.list(restoratives);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("药品订单详情", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), Restoratives.class).sheet("药品订单详情").doWrite(list);
    }

}

